package com.binarysoft.service.common.snmp;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class DefaultSnmpResult implements SnmpResult {

	private Collection<SnmpValue> values = new ArrayList<SnmpValue>();

	protected DefaultSnmpResult() {
		super();
	}

	protected DefaultSnmpResult(Collection<SnmpValue> values) {
		super();
		this.values.addAll(values);
	}

	public int size() {
		return this.values.size();
	}

	public Iterator<SnmpValue> iterator() {
		return values.iterator();
	}

	public synchronized Collection<String> getIndexes(String oid) {
		List<String> value = new ArrayList<String>();
		Iterator<SnmpValue> iter = values.iterator();
		while (iter.hasNext()) {
			SnmpValue snmpValue = iter.next();
			if (snmpValue.getOid().equals(oid)) {
				value.add(snmpValue.getIndex());
			}
		}
		return value;
	}

	public synchronized Collection<String> getIndexesByName(String objectName) {
		List<String> value = new ArrayList<String>();
		Iterator<SnmpValue> iter = values.iterator();
		while (iter.hasNext()) {
			SnmpValue snmpValue = iter.next();
			if (snmpValue.getObjectName().equals(objectName)) {
				value.add(snmpValue.getIndex());
			}
		}
		return value;
	}

	public synchronized void addValue(Collection<SnmpValue> values) {
		this.values.addAll(values);
	}

	public synchronized void addValue(SnmpValue value) {
		this.values.add(value);
	}

	public Collection<SnmpValue> getValues() {
		return new ArrayList<SnmpValue>(this.values);
	}

	public synchronized SnmpValue getValueByName(String objectName, String index) {
		SnmpValue val = null;
		Iterator<SnmpValue> iter = this.values.iterator();
		while (iter.hasNext()) {
			SnmpValue snmpValue = iter.next();
			if (snmpValue.getObjectName().equals(objectName) && snmpValue.getIndex().equals(index)) {
				val = snmpValue;
				break;
			}
		}
		return val;
	}

	public synchronized SnmpValue getValue(String oid, String index) {
		SnmpValue val = null;
		Iterator<SnmpValue> iter = this.values.iterator();
		while (iter.hasNext()) {
			SnmpValue snmpValue = iter.next();
			if (snmpValue.getOid().equals(oid) && snmpValue.getIndex().equals(index)) {
				val = snmpValue;
				break;
			}
		}
		return val;
	}

	public synchronized Collection<SnmpValue> getValues(String oid) {
		List<SnmpValue> value = new ArrayList<SnmpValue>();
		Iterator<SnmpValue> iter = this.values.iterator();
		while (iter.hasNext()) {
			SnmpValue snmpValue = iter.next();
			if (snmpValue.getOid().equals(oid)) {
				value.add(snmpValue);
			}
		}
		return value;
	}

	public synchronized Collection<SnmpValue> getValuesByName(String objectName) {
		List<SnmpValue> value = new ArrayList<SnmpValue>();
		Iterator<SnmpValue> iter = this.values.iterator();
		while (iter.hasNext()) {
			SnmpValue snmpValue = iter.next();
			if (snmpValue.getObjectName().equals(objectName)) {
				value.add(snmpValue);
			}
		}
		return value;
	}
}
